//
//  RainbowLamp.m
//  UI05_Rainbow Lamp
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RainbowLamp.h"
#import "UIColor+RandomColor.h"
#import "RainbowView.h"

@implementation RainbowLamp


-(instancetype)initWithFrame:(CGRect)frame withColorCounts:(NSInteger)colorCounts
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        NSMutableArray *colorArray = [NSMutableArray arrayWithCapacity:colorCounts];
        for (int i = 0; i < colorCounts; i ++) {
            [colorArray addObject:[UIColor randomColor]];
        }
        for (int i = 0; i < colorCounts; i ++) {
            RainbowView *view = [[RainbowView alloc]initWithFrame:CGRectMake(0 + i * 20, 0 + i * 20, self.frame.size.width - 40 * i, self.frame.size.height - 40 * i)];
            view.tag = 100 * i + i;
            view.backgroundColor = colorArray[i];
            [self addSubview:view];
            [view release];
        }
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(time) userInfo:nil repeats:YES];
    }
    
    return self;
}


-(void)time{
//    NSInteger *count = colorArray.count;
    NSArray *sub = [self subviews];
    UIColor *temp = [self viewWithTag:100 * sub.count].backgroundColor;
    
    for (int i = 0; i < sub.count -1; i ++) {
        [self viewWithTag:100 * sub.count + i].backgroundColor = [self viewWithTag:100 * sub.count + 1 + i].backgroundColor ;
    }
    [self viewWithTag:100 * sub.count + sub.count].backgroundColor = temp;
    
}

@end
